#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
| Created on 2020/3/25 15:10

| @author: aimee.mei
"""

import sys, logging,traceback
from util.db_util import execute_query


def get_lastest_product_chain_stream_id():
    '''
    查询产业链最新的一条id

    :return: id
    '''
    try:
        results = execute_query(
            "select max(id) from product_chain_stream order by updated_time desc")
        result = results[0][0]
        return result
    except Exception as e:
        logging.exception("Retrieve latest id failed!")
        logging.error(e)
        traceback.print_exc()
        return None


def get_upstream_code():
    '''
    查询最新的一条产业链上游upstream_code信息

    :return: upstream_code
    '''
    try:
        results = execute_query(
            "select upstream_code from product_chain_stream where id='%s'" % get_lastest_product_chain_stream_id())
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve upstream failed!")
        logging.error(e)
        traceback.print_exc()
        return None

#查询最新的一条产业链下游信息
def get_downstream_code():
    '''
    查询最新的一条产业链下游downstream_code信息

    :return: downstream_code
    '''
    try:
        results = execute_query(
            "select downstream_code from product_chain_stream where id='%s'" % get_lastest_product_chain_stream_id())
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve downstream failed!")
        logging.error(e)
        traceback.print_exc()
        return None




def get_scm_product_code(name):
    '''
    查询scm_productcode表节点code

    :param name: 节点name

    :return: code
    '''
    try:
        results = execute_query(
            "SELECT code FROM `scm_product` where name_zh='%s' ORDER BY created_time desc LIMIT 1;" % name)
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve downstream failed!")
        logging.error(e)
        traceback.print_exc()
        return None